package org.example.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.example.config.SentineHandlers;
import org.example.service.ResumeServiceFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author ：xjlonly
 * @date ：Created in 2021/4/6 17:12
 * @description：控制器
 * @modified By：
 * @version: 1.0$
 */
@RestController
@RequestMapping("/autodeliver")
public class AutoDeliverController {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ResumeServiceFeignClient resumeServiceFeignClient;

    /**
    * @SentinelResource
    value：定义资源名
    blockHandlerClass：指定Sentinel规则异常兜底逻辑所在class类
    blockHandler：指定Sentinel规则异常兜底逻辑具体哪个⽅法
    fallbackClass：指定Java运⾏时异常兜底逻辑所在class类
    fallback：指定Java运⾏时异常兜底逻辑具体哪个⽅法
    */
    @GetMapping("/checkState/{userId}")
    @SentinelResource(value = "findResumeOpenState",
            blockHandlerClass = SentineHandlers.class, blockHandler = "handleException",
            fallbackClass = SentineHandlers.class, fallback = "handleError"
    )
    public Integer findResumeOpenState(@PathVariable Long userId){
        //ribbon负载均衡自动根据服务名选择相应的服务
        final Integer resumeOpenState = resumeServiceFeignClient.findResumeOpenState(userId);
        // 模拟降级：异常⽐例
        System.out.println("======>>>调⽤简历微服务，获取到⽤户" + userId + "的默认简历当前状态为：" + resumeOpenState);
        return resumeOpenState;
    }


    //定义回退方法 返回预设默认值
    //注意：该方法形参和返回值与原始方法保持一致
    public Integer myFallBack(Long userId){
        return -1; //兜底数据
    }
}
